var CONFIG = require("utils/config.js")
var Bmob = require('utils/bmob.js');
Bmob.initialize("82afc0b00241f05178aa3c2fea2b07cc", "cfdea8c0d5d18aa37ef488d2e394d4a3");
var that
App({
  onLaunch: function () {
 
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
 

    var that=this
    that.login();
    // 登录
  },
  // login: function () {
  //   that = this
  //   wx.login({
  //     //登录成
  //     success: function (res) {

  //       var code = res.code
  //       // console.log(res)
  //       console.log(code)

  //       wx.getUserInfo({
  //         success: function (res) {
  //           console.log("===ces--")
  //           console.log(res)
  //           that.getinfo(code)
  //           // that.myopenid(code)

  //         },
  //         fail: function (err) {
  //           wx.openSetting({
  //             success: (res) => {
  //               console.log(res)
  //               console.log("测试在faileId")
  //               if (res.authSetting["scope.userInfo"] === true) {
  //                 console.log("设置 测试")

  //                 wx.login({
  //                   success: function (res) {
  //                     console.log(res)
  //                     that.getinfo(res.code)
  //                   }
  //                 })
  //                 //更新数据
  //               }

  //             }
  //           })
  //         },
  //         complete: function (res) {
  //           console.log(res)
  //          wx.setStorageSync('userInfo', res.userInfo)

  //         }

  //       })



  //     },
  //     //拒绝授权 发起请求
  //     fail: function (err) {
  //       wx.openSetting({
  //         success: (res) => {
  //           console.log(res)
  //           console.log("测试在faileId")
  //           if (res.authSetting["scope.userInfo"] === true) {
  //             console.log("设置 测试")

  //             wx.login({
  //               success: function (res) {
  //                 console.log(res)
  //                 that.getinfo(res.code)
  //               }
  //             })
  //             //更新数据
  //           } else {
  //             wx.login({
  //               success: function (res) {
  //                 console.log(res)
  //                 that.getinfo(res.code)
  //               },
  //               fail: function (err) {
  //                 wx.showModal({
  //                   title: '授权失败',
  //                   content: '',
  //                 })
  //               }
  //             })
  //           }

  //         }
  //       })
  //     }
  //   })



  // },

  login: function () {
    var that = this
    wx.login({
      success: function (res) {
        var code = res.code
        wx.getUserInfo({
          success: function (res) {
            that.zhuce(code)
          },
          fail: function (err) {
            console.log("===")
            wx.openSetting({
              success: (res) => {
                console.log(res)
                console.log("测试在faileId")
                if (res.authSetting["scope.userInfo"] === true) {
                  console.log("设置 测试")

                  wx.login({
                    success: function (res) {
                      console.log(res)
                      that.zhuce(res.code)
                    }
                  })
                  //更新数据



                }

              }
            })
          }
        })
      }
    })

  },


  zhuce: function (code) {

    var User = new Bmob.User;



    if (code) {
      Bmob.User.requestOpenId(code, {//获取userData(根据个人的需要，如果需要获取userData的需要在应用密钥中配置你的微信小程序AppId和AppSecret，且在你的项目中要填写你的appId)
        success: function (userData) {
          wx.getUserInfo({
            success: function (result) {
              var that = this;
              console.log(result)
              var userInfo = result.userInfo
              var nickName = userInfo.nickName
              var user = new Bmob.User();//开始注册用户
              user.set("username", nickName);
              user.set("uimg", userInfo.avatarUrl);
              wx.setStorageSync("openId", userData.openid);
              wx.setStorageSync("myname", nickName);
              wx.setStorageSync("userInfo", userInfo);
              wx.setStorageSync("myurl", userInfo.avatarUrl);
              user.set("cishu", 0);
              user.set("openid", userData.openid);
              console.log("opeenidwei==" + userData.openid);
              user.set("password", userData.openid);//因为密码必须提供，但是微信直接登录小程序是没有密码的，所以用openId作为唯一密码
              user.set("userData", userData);
              user.signUp(null, {
                success: function (res) {
                  console.log("注册成功!");
                },
                error: function (userData, error) {
                  console.log(error)
                  if (error.code == 202) {
                    Bmob.User.logIn(userData.get('username'), userData.get('password'), {
                      success: function (user) {
                        console.log("手动登录");

                      

                      },
                      error: function (user, error) {
                        console.log("测试失败")
                      }


                    })
                  }
                }
              });
            },
            fail: function (err) {
              console.log("====")

              wx.showModal({
                title: '温馨提醒',
                content: '您没有授权哦~',
              })




            }
          })
        },
        error: function (error) {
          // Show the error message somewhere
          console.log("拒绝1")
          console.log("Error: " + error.code + " " + error.message);
        }
      });

    } else {
      console.log('获取用户登录态失败！' + res.errMsg)
    }






  },
  globalData: {
    speedNum:1,
   booktitle:'神经小说2',
   allPage:1,
    bookinfo:{}
  },  
  getinfo:function(code){
    var that = this
    var url = "https://api.weixin.qq.com/sns/jscode2session"

    var data = {
      "appid": "wx041268bb60d928ec",
      "secret":"9e7f7580b2eb3785cc1879547a0a8d83",
      "js_code":code,
      "grant_type":"authorization_code"
    };
    CONFIG._getdata(url, data, function (res) {
      console.log("---用户数据")
   
      var openId=res.data.openid
      var userInfo=wx.getStorageSync('userInfo')
      var username=userInfo.nickName
      var headurl=userInfo.avatarUrl
      // console.log("头像++"+headurl)
      // console.log(userInfo)
     
      if(openId && username){
        that.insertuser(openId,username,headurl);
      }
    })
  },
  insertuser:function(openId,username,headurl){
    var that=this
    var data={
      uid:openId,
      nickname:username,
      headurl:headurl
    }
    var url = CONFIG.URL.insertuser
    CONFIG.post_from(url, data, function (res) {
      console.log(res.data)
      console.log("===注册登录成功==");
      if (res.data.code == "1") {
        wx.setStorageSync('openId', openId)
        
      }
    }, function (err) {
      if (err) {
        console.log(err)
      }
    })
  },

  showDetail: function (id,viewInfo,process) {
    var that = this
    var bookid = id
    console.log(bookid)
    var uid = wx.getStorageSync('openId')
    console.log(uid)
    if (!uid) {
      app.login()
    } else {
    
      wx.showLoading({
        title: '加载中~',
      })
      setTimeout(function () {
        wx.hideLoading();
        wx.navigateTo({
          url: '../bookinfo/index?id=' + bookid+'&viewInfo='+viewInfo+'&process='+process,
        })
      }, 1500);
    }
  },


})